-
Notifications
You must be signed in to change notification settings - Fork 5.3k
clippy: Removes redundant async blocks #31526
Conversation
Codecov Report
@@ Coverage Diff @@
## master #31526 +/- ##
=========================================
- Coverage 81.4% 81.3% -0.1%
=========================================
Files 731 731
Lines 205088 205084 -4
=========================================
- Hits 166944 166914 -30
- Misses 38144 38170 +26 |
Out of curiosity, why is the example in the description not one of the instances fixed in this PR? |
Heh, good eye! Here's the output from running
There were a few files like this too. So looks like Do you think I should manually add the changes from |
let _lock = ASYNC_TASK_SEMAPHORE.acquire(); | ||
let inner = self.inner.clone(); | ||
|
||
let _handle = RUNTIME.spawn(async move { send_data_async(inner, data).await }); | ||
let _handle = RUNTIME.spawn(send_data_async(inner, data)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this change affect behavior? I see we grab an async semaphore at the top of the function; does not running the send_data_async
in an async block break anything?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it does not
async fn send_data_async(
connection: Arc<NonblockingQuicConnection>,
buffer: Vec<u8>,
) -> TransportResult<()> {
Since send_data_async
is an async fn
, when you call it what happens is that it builds its return future and returns it. It only builds it tho, it does not poll it. Futures don't make progress until they're polled, so effectively the future will start executing only inside the task it's spawned on.
It would be different if send_data_async
built the future manually, eg:
fn send_data_async(
connection: Arc<NonblockingQuicConnection>,
buffer: Vec<u8>,
) -> SendDataFuture {
do_something_before_creating_the_future();
SendDataFuture {
...
}
}
In this case do_something_before_creating_the_future
does execute before the resulting future is polled. So before landing this we need to double check if we're creating any manual futures, and if we're doing actual work (eg locking things) before returning them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation!
I went through and audited all functions that were inside the async { .await }
blocks, and they all were async fn
(i.e. not creating any manual futures. So I think that means this PR is ok.
I plan on merging this PR tomorrow unless there are any objections. |
Either is fine with me. |
I've made a similar fix here: #30808 I wonder if after applying this PR you actually see all the Clippy warnings about redundant |
I would expect the same warnings to still exist in |
Problem
Clippy throws warnings on the next nightly version (1.70.0), which blocks upgrading our nightly Rust (see #31381).
Here's an example:
Summary of Changes
Remove redundant async away blocks